package com.section4.dao.Impl;

import com.section4.dao.DaoHelper;
import com.section4.dao.StudentDao;
import com.section4.entity.Student;
import com.section4.entity.UpdateStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private DaoHelper helper;

    private final String tableName = "student";
    private final String baseField = "(sid,name,english_name,address,phone_number,birthday)";

    @Override
    public int insert(Student student) {
        String sql= "insert into " + tableName + baseField +
                " values (" + "'" + student.getSid()
                + "','" + student.getName()
                + "','" + student.getEnglishName()
                + "','" + student.getAddress()
                + "'," + student.getPhoneNumber()
                + ",'" + student.getBirthday()
                + "');";
        return helper.insert(sql);
    }

    @Override
    public int update(UpdateStudent student) {
        String sql="update student " +
                "set name='"+student.getNewName()+"' , english_name='"+student.getNewEnglishName()+"' , address='"+student.getNewAddress()+"' , phone_number='"+student.getNewPhoneNumber()+"' , birthday='"+student.getNewBirthday()+"'" +
                "where name='"+student.getLaterName()+"' and english_name='"+student.getLaterEnglishName()+"' and address='"+student.getLaterAddress()+"' and  phone_number='"+student.getLaterPhoneNumber()+"' and birthday='"+student.getLaterBirthday()+"'; ";
        return helper.update(sql);
    }
}

